﻿using System;
using System.Text;

using Bpms.Config;

namespace Bpms.OrderStrategy
{
    /// <summary>
    /// 订单提供者
    /// </summary>
    public class OrderStrategyProvider
    {

        private static IOrderStrategy _instance = null;
        private static object lockHelper = new object();

        private OrderStrategyProvider()
        {

        }

        static OrderStrategyProvider()
        {
            //GetProvider();
        }

        private static void GetProvider(OrderType orderType)
        {
            try
            {
                _instance = (IOrderStrategy)Activator.CreateInstance(Type.GetType(string.Format("Bpms.Data.{0}.{1}, Data.{0}", ConfigContext.GetDbType, orderType.ToString()), true, true));
            }
            catch
            {
                throw new Exception("请检查database.config中dbType节点数据库类型是否正确，例如：SqlServer、Access、MySql");
            }
        }


        /// <summary>
        /// 得到指定类型的Provider
        /// </summary>
        /// <param name="orderType">订单类型</param>
        /// <returns>IOrderStrategy 类型</returns>
        public static IOrderStrategy GetInstance(OrderType orderType)
        {
            if (_instance == null)
            {
                lock (lockHelper)
                {
                    if (_instance == null)
                    {
                        GetProvider(orderType);
                    }
                }
            }

            return _instance;
        }

        /// <summary>
        /// 重新设置前调用
        /// </summary>
        public static void ResetDbProvider()
        {
            _instance = null;
        }
    }

    /// <summary>
    /// 订单类型
    /// </summary>
    public enum OrderType
    {
        Customer,//客服
        Design,//设计
        Technological,//技术
        Translation//翻译
    }
}
